<?php
function checkillegalword($url){
	$words=array();
	$words[]="`";
	$words[]="'";
	$words[]="delete";
	$words[]="cteate";
	$words[]="drop";
	$words[]="select";
	$words[]="where";
	$words[]="--";
	$words[]="or";
	$words[]="=";
	$words[]="*";
	foreach($_POST as $strGot){
		$strGot=strtolower($strGot);
		foreach($words as $word){
			if(strstr($strGot,$word)){
				ShowMsg("非法字符提交!",$url);
				exit;
			}
		}
	}
}

//获得当前的脚本网址
function GetCurUrl()
{
	if(!empty($_SERVER["REQUEST_URI"]))
	{
		$scriptName = $_SERVER["REQUEST_URI"];
		$nowurl = $scriptName;
	}
	else
	{
		$scriptName = $_SERVER["PHP_SELF"];
		if(empty($_SERVER["QUERY_STRING"]))
		{
			$nowurl = $scriptName;
		}
		else
		{
			$nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
		}
	}
	return $nowurl;
}

function guestip() { //获取客户端ip
if (getenv('HTTP_CLIENT_IP')) { 
$ip = getenv('HTTP_CLIENT_IP'); 
} 
elseif (getenv('HTTP_X_FORWARDED_FOR')) { 
$ip = getenv('HTTP_X_FORWARDED_FOR'); 
} 
elseif (getenv('HTTP_X_FORWARDED')) { 
$ip = getenv('HTTP_X_FORWARDED'); 
} 
elseif (getenv('HTTP_FORWARDED_FOR')) { 
$ip = getenv('HTTP_FORWARDED_FOR'); 

} 
elseif (getenv('HTTP_FORWARDED')) { 
$ip = getenv('HTTP_FORWARDED'); 
} 
else { 
$ip = $_SERVER['REMOTE_ADDR']; 
} 
return $ip; 
} 
//获取IP
function GetIP()
{
	if(!empty($_SERVER["HTTP_CLIENT_IP"]))
	{
		$cip = $_SERVER["HTTP_CLIENT_IP"];
	}
	else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
	{
		$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	}
	else if(!empty($_SERVER["REMOTE_ADDR"]))
	{
		$cip = $_SERVER["REMOTE_ADDR"];
	}
	else
	{
		$cip = '';
	}
	preg_match("/[\d\.]{7,15}/", $cip, $cips);
	$cip = isset($cips[0]) ? $cips[0] : 'unknown';
	unset($cips);
	return $cip;
}

//获得GD的版本
function gdversion()
{
	//没启用php.ini函数的情况下如果有GD默认视作2.0以上版本
	if(!function_exists('phpinfo'))
	{
		if(function_exists('imagecreate'))
		{
			return '2.0';
		}
		else
		{
			return 0;
		}
	}
	else
	{
		ob_start();
		phpinfo(8);
		$module_info = ob_get_contents();
		ob_end_clean();
		if(preg_match("/\bgd\s+version\b[^\d\n\r]+?([\d\.]+)/i", $module_info,$matches))
		{
			$gdversion_h = $matches[1];
		}
		else
		{
			$gdversion_h = 0;
		}
		return $gdversion_h;
	}
}

//html转成text文本
function html2text($str)
{
	$str = preg_replace("/<sty(.*)\\/style>|<scr(.*)\\/script>|<!--(.*)-->/isU","",$str);
	$alltext = "";
	$start = 1;
	for($i=0;$i<strlen($str);$i++)
	{
		if($start==0 && $str[$i]==">")
		{
			$start = 1;
		}
		else if($start==1)
		{
			if($str[$i]=="<")
			{
				$start = 0;
				$alltext .= " ";
			}
			else if(ord($str[$i])>31)
			{
				$alltext .= $str[$i];
			}
		}
	}
	$alltext = str_replace("　"," ",$alltext);
	$alltext = preg_replace("/&([^;&]*)(;|&)/","",$alltext);
	$alltext = preg_replace("/[ ]+/s"," ",$alltext);
	return $alltext;
}

//去除空格和html标签
function excode($o){
	$v=trim($o);
	$v=html2text($v);
	//$v=htmlentities($v);
	return $v;
}

//半角引号转换成全角引号
function exquot($str){
	//$temp=stripslashes($str);
	//$temp=str_replace("\"","“",$temp);
	//return str_replace("'","‘",$temp);
	return htmlspecialchars($str,ENT_QUOTES);
}

//半角引号转换成全角引号1
function exquot1($str){
	//$temp=stripslashes($str);
	//$temp=str_replace("\"","“",$temp);
	//return str_replace("'","‘",$temp);
	$temp=stripslashes($str);
	return htmlspecialchars($temp,ENT_QUOTES);
	
}

//得到扩展名
function getExtension($str){
	$strE=substr($str,strrpos($str,".")+1);
	if(eregi("^[a-z]{1,4}$",$strE)){//扩展名为1到4位的英文字符，忽略大小写(指我们所需的扩展名)
		$strE=strtolower($strE);
		return $strE;
	}else{
		return "";
	}
}

function ShowMsg($msg,$gurl){
	
	$pghead="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
	$pghead.="<html>\r\n<head>\r\n<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />\r\n<title>系统提示</title>\r\n</head>\r\n<body>\r\n";
	$pgfoot="</body>\r\n<script  type='text/javascript'>setTimeout('window.history.back()',2000)</script>\r\n</html>";
	
	$pgmsg="<div style='width:500px; height:250px; margin:0 auto; background-color:#eee; border:#CCC 1px dashed;'>
	<div style='width:100%; height:26px; line-height:26px; background-color:#39F; text-align:center; font-size:12px; color:#609;'><b>系统提示</b></div>
    <div style='margin-top:60px; font-size:12px; text-align:center; line-height:30px;'>
		".$msg."
    </div>
    <div style='margin-top:15px; text-align:center; font-size:12px;'><a href='".$gurl."'>浏览器没有反应，点此返回！</a></div>
</div>";

echo $pghead.$pgmsg.$pgfoot;
}

function ShowMsg_h($msg,$gurl){
	
	$pghead="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
	$pghead.="<html>\r\n<head>\r\n<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />\r\n<title>系统提示</title>\r\n</head>\r\n<body>\r\n";
	$pgfoot="</body>\r\n<script  type='text/javascript'>setTimeout(\"window.location.href='".$gurl."'\",2000)</script>\r\n</html>";
	
	$pgmsg="<div style='width:500px; height:250px; margin:0 auto; background-color:#eee; border:#CCC 1px dashed;'>
	<div style='width:100%; height:26px; line-height:26px; background-color:#39F; text-align:center; font-size:12px; color:#609;'><b>系统提示</b></div>
    <div style='margin-top:60px; font-size:12px; text-align:center; line-height:30px;'>
		".$msg."
    </div>
    <div style='margin-top:15px; text-align:center; font-size:12px;'><a href='".$gurl."' >系统正在跳转,如果浏览器没有反应，点此进入！</a></div>
</div>";

echo $pghead.$pgmsg.$pgfoot;
}

//是否为空
function isempty($str){
 if($str==""){
   $str="老琦企业建站系统";	
 }
 return $str;
}

function islogintxt($str){//检测是否为0-9a-z和下划线的字符串
	if(preg_match("/^\w+$/i",$str)){
		return 1;
	}else{
		return 0;
	}
}

//是否为数字
function isnum($str,$msg,$gurl){
	if(!is_numeric($str)){
	ShowMsg($msg,$gurl);
	exit;
	}
}

function select_sql($t,$f,$w){
 $o=" SELECT ".$t." FROM ".$f." WHERE ".$w;
 return $o;
}
function update_sql($t,$k,$w){
 $o=" UPDATE ".$t." SET ".$k." WHERE ".$w;
 return $o;
}
function insert_sql($t,$k,$v){
 $o=" INSERT INTO ".$t." (".$k.") VALUES (".$v.")";
 return $o;
}
?>